Page({
  data: {
    weekDays: ['日', '一', '二', '三', '四', '五', '六'],
    currentYear: new Date().getFullYear(),
    currentMonth: new Date().getMonth() + 1,
    days: []
  },

  onLoad() {
    this.generateCalendar();
  },

  // 生成日历数据
  generateCalendar() {
    const { currentYear, currentMonth } = this.data;
    const days = [];
    
    // 当月天数
    const daysInMonth = new Date(currentYear, currentMonth, 0).getDate();
    
    // 当月第一天是星期几
    const firstDay = new Date(currentYear, currentMonth - 1, 1).getDay();
    
    // 上个月最后几天
    const prevMonthDays = new Date(currentYear, currentMonth - 1, 0).getDate();
    for (let i = firstDay - 1; i >= 0; i--) {
      days.push({
        day: prevMonthDays - i,
        isCurrentMonth: false,
        isToday: false,
        date: `${currentYear}-${currentMonth - 1}-${prevMonthDays - i}`
      });
    }
    
    // 当月日期
    const today = new Date();
    for (let i = 1; i <= daysInMonth; i++) {
      const isToday = currentYear === today.getFullYear() && 
                     currentMonth === today.getMonth() + 1 && 
                     i === today.getDate();
      days.push({
        day: i,
        isCurrentMonth: true,
        isToday,
        date: `${currentYear}-${currentMonth}-${i}`
      });
    }
    
    // 下个月前几天
    const totalCells = Math.ceil(days.length / 7) * 7;
    for (let i = 1; i <= totalCells - days.length; i++) {
      days.push({
        day: i,
        isCurrentMonth: false,
        isToday: false,
        date: `${currentYear}-${currentMonth + 1}-${i}`
      });
    }
    
    this.setData({ days });
  },

  // 上个月
  prevMonth() {
    let { currentYear, currentMonth } = this.data;
    if (currentMonth === 1) {
      currentYear--;
      currentMonth = 12;
    } else {
      currentMonth--;
    }
    this.setData({ currentYear, currentMonth }, () => {
      this.generateCalendar();
    });
  },

  // 下个月
  nextMonth() {
    let { currentYear, currentMonth } = this.data;
    if (currentMonth === 12) {
      currentYear++;
      currentMonth = 1;
    } else {
      currentMonth++;
    }
    this.setData({ currentYear, currentMonth }, () => {
      this.generateCalendar();
    });
  },

  // 选择日期
  selectDay(e) {
    const selectedDate = e.currentTarget.dataset.date;
    wx.showToast({
      title: `选择了 ${selectedDate}`,
      icon: 'none'
    });
    // 这里可以添加跳转到日程详情等逻辑
  }
});